package com.lz.common.utils;

import cn.hutool.captcha.*;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import java.io.File;

public class CaptchaType {

    public static void main(String[] args) {
        AbstractCaptcha abstractCaptcha = generateMathExpressionCaptcha(new File("E:/test/captcha.png"));
        System.out.println(abstractCaptcha.getCode());
    }

    /**
     * 生成线型验证码图片
     *
     * @param file 输出文件
     */
    public static AbstractCaptcha generateLineCaptcha(File file) {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        lineCaptcha.write(file);
        return lineCaptcha;
    }

    /**
     * 生成圆形干扰验证码图片
     *
     * @param file 输出文件
     */
    public static AbstractCaptcha generateCircleCaptcha(File file) {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 30);
        captcha.write(file);
        return captcha;
    }

    /**
     * 生成GIF动态验证码
     *
     * @param file 输出文件
     */
    public static AbstractCaptcha generateGifCaptcha(File file) {
        GifCaptcha captcha = CaptchaUtil.createGifCaptcha(200, 100);
        captcha.write(file);
        return captcha;
    }

    /**
     * 生成纯数字验证码
     *
     * @param file 输出文件
     */
    public static AbstractCaptcha generateNumericCaptcha(File file) {
        RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        lineCaptcha.setGenerator(randomGenerator);
        lineCaptcha.createCode();
        lineCaptcha.write(file);
        return lineCaptcha;
    }

    /**
     * 生成数学表达式验证码（四则运算）
     *
     * @param file 输出文件
     */
    public static AbstractCaptcha generateMathExpressionCaptcha(File file) {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        lineCaptcha.setGenerator(new MathGenerator());
        lineCaptcha.createCode();
        lineCaptcha.createCode(); // 多次生成以确保随机性
        lineCaptcha.write(file);
        return lineCaptcha;
    }
}
